import moment from 'moment';
import { acceptHMRUpdate, defineStore } from 'pinia';

interface BindShopListType {
  createTime: string;
  pType: number;
  shopId: string;
  shopName: string;
  status: number;
  tbId: number;
  updateTime: string;
  userId: number;
}

interface Auto1688State {
  bindShopList: any[];
  // bindMerChantBuy: any | null;
  bind1688: boolean;
  binddy: boolean;
  binddyorder: boolean;
  nowSelectDyShop: any;
  addShopIdList: BindShopListType[];
}

export const auto1688Store = defineStore('auto-1688-store', {
  actions: {
    set1688BindShop(typeList: any, user_id?: any) {
      this.bindShopList = typeList?.shopList || [];
      // this.bindMerChantBuy = typeList.merchantBuy;
      typeList?.shopList?.forEach((item: any) => {
        item.isvOpenPay = item.isvOpenPay * 1;
        if (item.pType === 1 && item.status === 1 && item.isUseing === 1) {
          this.bind1688 = true;
        }
        if (item.pType === 2) {
          this.binddy = true;
        }
        if (item.pType === 3) {
          // 需要根据所选的店铺进行判断
          this.binddyorder = true;
        }
        const expireTime = moment(item.expireTime);
        const today = moment();
        const days = expireTime.diff(today, 'days');
        // item.expireTime = days;
        item.remainingTain = days;
      });
      const getUseShopId = localStorage.getItem('useSelectDyShop');
      if (getUseShopId) {
        const findInListId =
          this.bindShopList &&
          this.bindShopList.find((item: any) => item.shopId === getUseShopId);
        this.nowSelectDyShop =
          findInListId ||
          (this.bindShopList &&
            this.bindShopList.find((item: any) => item.pType === 2));
        localStorage.setItem('useSelectDyShop', this.nowSelectDyShop?.shopId);
      } else {
        this.nowSelectDyShop =
          this.bindShopList &&
          this.bindShopList.find((item: BindShopListType) => item.pType === 2);
        localStorage.setItem('useSelectDyShop', this.nowSelectDyShop?.shopId);
      }
      if (user_id) {
        const findInListId =
          this.bindShopList &&
          this.bindShopList.find(
            (item: any) => item.userId === user_id && item.pType === 2,
          );
        this.nowSelectDyShop =
          findInListId ||
          (this.bindShopList &&
            this.bindShopList.find((item: any) => item.pType === 2));
        localStorage.setItem('useSelectDyShop', this.nowSelectDyShop?.shopId);
      }
      const shopAppListFormAter = typeList?.shopList?.filter(
        (item: any) => item.pType === 2 || item.pType === 3,
      );
      // 查找并保存出唯一的店铺id
      const addShopIdList: any = [];
      shopAppListFormAter?.forEach((item: any) => {
        const getIsShopData = addShopIdList.find(
          (item2: any) => item2.shopId === item.shopId,
        );
        if (getIsShopData) {
          getIsShopData.shopList.push(item);
        } else {
          addShopIdList.push({
            shopId: item.shopId,
            shopName: item.shopName,
            shopList: [item],
          });
        }
      });
      // console.log('addShopIdList', addShopIdList);
      this.addShopIdList = addShopIdList.map((item: any) => {
        item.dypuhuo = item.shopList.find((item2: any) => item2.pType === 2);
        item.dyorder = item.shopList.find((item2: any) => item2.pType === 3);
        return item;
      });
    },
    setSelectNowShop(shopId: string) {
      // console.log('this.nowSelectDyShop', shopId);
      this.nowSelectDyShop = this.bindShopList?.find(
        (item: BindShopListType) => item.shopId === shopId,
      );
      localStorage.setItem('useSelectDyShop', shopId);
    },
  },
  state: (): Auto1688State => ({
    bindShopList: [],
    // bindMerChantBuy: {},
    bind1688: false,
    binddy: false,
    binddyorder: false,
    nowSelectDyShop: {},
    addShopIdList: [],
  }),
});

// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
  hot.accept(acceptHMRUpdate(auto1688Store, hot));
}
